package com.westvalley.workflow.action.nc;

import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.action.BasisWe8Action;
import com.westvalley.common.action.result.ActionResult;
import com.westvalley.common.action.util.WorkFlowActionUtil;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.result.JsonResult;
import com.westvalley.service.api.nc.ReimbursementService;
import com.westvalley.service.api.nc.bean.ExpenseDoc;
import com.westvalley.service.api.nc.bean.ExpenseDocBody;
import com.westvalley.service.api.nc.bean.ExpenseDocBodyItem;
import com.westvalley.service.api.nc.bean.ExpenseDocHead;
import com.westvalley.service.api.nc.bean.ExpenseDocOffset;
import com.westvalley.service.api.nc.bean.ExpenseDocOffsetItem;

public class C001CreateAction extends BasisWe8Action{

	@Override
	protected ActionResult executeResult(RequestInfo requestInfo) {
		ActionResult result = ActionResult.me(requestInfo);
		JsonResult jsonResult = ReimbursementService.me(initBean(requestInfo)).getJsonResult();
		if(!jsonResult.isFlag())
			throw new RuntimeException(jsonResult.getMsg());
		else{
			JSONObject resultJson = (JSONObject) jsonResult.getResult();
			String budgetExceed = resultJson.getString("budgetExceed");
			String budgetType = StringKit.equals("N", budgetExceed) ? "0" : "1";
			Db.update("UPDATE "+mainTableName+" SET ncSingleNo = ?, isBudgetExceed = ?, yuslx = ? WHERE requestId = ?", resultJson.getString("message"), budgetExceed, budgetType,requestInfo.getRequestid());
		}
		return result;
	}
	
	
	private ExpenseDoc initBean(RequestInfo requestInfo){
		ExpenseDoc bean = new ExpenseDoc();
		ExpenseDocHead head = new ExpenseDocHead();
		MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
		//NC单号
		head.setNcbillno(WorkFlowActionUtil.getFieldValue(mainTableInfo, "ncSingleNo"));
		//公司编码
		head.setUnitcode(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeunitcode"));
		//交易类型
		head.setBilltypecode(WorkFlowActionUtil.getFieldValue(mainTableInfo, "billType"));
		//制单人
		head.setLrr("oalrr01");
		//单据日期
		head.setDjrq(WorkFlowActionUtil.getFieldValue(mainTableInfo, "tianbrq"));
		//业务摘要
		head.setZy(WorkFlowActionUtil.getFieldValue(mainTableInfo, "ywzy"));
		//币种
		head.setBzbm("CNY");
		//汇率
		head.setBbhl("1.00");
		//审核日期
		head.setShrq(StringKit.getCurrentDate());
		//附件张数
		head.setFjzs(WorkFlowActionUtil.getFieldValue(mainTableInfo, "fujzs"));
		//报销公司
		head.setZfdwbm(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeunitcode"));
		//公司银行帐号
		Record unitBank = Db.findFirst("SELECT * FROM WV_NC_UNIT_BANKS WHERE ID = ?",WorkFlowActionUtil.getFieldValue(mainTableInfo, "unitbank"));
		if(unitBank!=null)
			head.setFkyhzh(StringKit.null2o(unitBank.get("ACCOUNTCODE")));
		//结算方式
		head.setJsfs(WorkFlowActionUtil.getFieldValue(mainTableInfo, "jiesfs"));
		//费用承担公司
		Record unitinfo = Db.findFirst("SELECT * FROM WV_NC_UNIT WHERE PK_CORP = ?",WorkFlowActionUtil.getFieldValue(mainTableInfo, "costunit"));
		if(unitinfo!=null)
			head.setFydwbm(StringKit.null2o(unitinfo.get("UNITCODE")));
		//费用承担部门
		Record deptinfo = Db.findFirst("SELECT * FROM WV_NC_DEPT WHERE PK_DEPTDOC = ?",WorkFlowActionUtil.getFieldValue(mainTableInfo, "costdept"));
		if(deptinfo!=null)
			head.setFydeptcode(StringKit.null2o(deptinfo.get("DEPTCODE")));
		//客商编码
		Record clientinfo = Db.findFirst("SELECT * FROM WV_NC_CLIENT WHERE PK_CUBASDOC = ?",WorkFlowActionUtil.getFieldValue(mainTableInfo, "costclient"));
		if(clientinfo!=null)
			head.setCustcode(StringKit.null2o(clientinfo.get("CUSTCODE")));
		//报销人公司
		head.setDwbm(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeunitcode"));
		//报销人
		head.setJkbxr(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeworkcode"));
		//报销人部门
		head.setBxrdeptcode(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financedeptcode"));
		//收款人
		head.setReceiver(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeworkcode"));
		//收款人银行账号
		Record psnBanks = Db.findFirst("SELECT * FROM WV_NC_PSNDOC_BANK WHERE ID = ?",WorkFlowActionUtil.getFieldValue(mainTableInfo, "financebank"));
		if(psnBanks!=null)
			head.setSkyhzh(StringKit.null2o(psnBanks.get("ACCOUNTCODE")));
		//OA单据号
		head.setOano(requestInfo.getRequestid());
		bean.setHead(head);
		
		//遍历开支明细
		DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(0);
		ExpenseDocBody body = new ExpenseDocBody();
		for(Row row : detailTable.getRow()){
			ExpenseDocBodyItem item = new ExpenseDocBodyItem();
			//收支项目
			Record incomeInfo = Db.findFirst("SELECT * FROM WV_NC_INCOME WHERE PK_COSTSUBJ = ?", WorkFlowActionUtil.getFieldValue(row, "income"));
			if(incomeInfo!=null)
				item.setSzxm(StringKit.null2o(incomeInfo.get("COSTCODE")));
			//报销金额
			item.setBxje(WorkFlowActionUtil.getFieldValue(row, "baoxje"));
			//报销事由
			item.setBxsy(WorkFlowActionUtil.getFieldValue(row, "baoxsy"));
			body.add(item);
		}
		bean.setBody(body);
		
		//遍历冲销明细
		DetailTable detailTableReversal = requestInfo.getDetailTableInfo().getDetailTable(1);
		ExpenseDocOffset offset = new ExpenseDocOffset();
		for(Row row : detailTableReversal.getRow()){
			ExpenseDocOffsetItem item = new ExpenseDocOffsetItem();
			//NC借款单主键
			item.setNcbillpk(WorkFlowActionUtil.getFieldValue(row, "loanpk"));
			//冲销原币金额
			item.setCjkybje(WorkFlowActionUtil.getFieldValue(row, "chongxybje"));
			//冲销日期
			item.setCxrq(WorkFlowActionUtil.getFieldValue(row, "chongxrq"));
			offset.add(item);
		}
		bean.setOffset(offset);
		return bean;
	}
}
